package com.java67.CarRent;

import com.wxws.sms.VIP;

public class CarManager {

	String name = "神船租车";
	Car[] car = new Car[20];

	/**
	 * 判断车辆是否已经存在
	 */
	public boolean checkCarName(String carName) {
		for (int i = 0; i < car.length; i++) {
			if (car[i] != null && carName.equals(car[i].name)) {
				return true;
			}
		}
		return false;
	}

	/**
	 * 加入车辆的方法
	 * 
	 */
	public boolean addCar(String name, double price) {

		if (checkCarName(name)) {
			System.out.println("车牌号不能重复!");
			return false;
		}
		for (int i = 0; i < car.length; i++) {
			if (car[i] == null) {
				car[i] = new Car();
				car[i].name = name;
				car[i].price = price;
				return true;
			}
		}
		return false;
	}

	/**
	 * 展示车辆信息
	 */
	public void showCarInfor() {
		System.out.println("序号\t汽车信息\t状态\t借出天数\t借出次数\t日租金");
		for (int i = 0; i < car.length; i++) {
			if (car[i] != null) {
				System.out.print((i + 1) + "、   ");
				car[i].show();

			}
		}
	}

	/**
	 * 淘汰车辆
	 */
	public boolean delCar(String name) {
		if (!checkCarName(name)) {
			System.out.println("没找到该车牌号!");
			return false;
		}
		for (int i = 0; i < car.length; i++) {
			if (car[i] != null && name.equals(car[i].name)) {
				if (car[i].state == false) {
					car[i] = null;
				} else {
					System.out.println("此车已租出！");
					return false;
				}
			}
		}
		for (int i = 0; i < car.length; i++) {
			if (car[i] == null) {
				for (int j = i; j < car.length; j++) {
					if (car[j] != null) {
						car[i] = car[j];
						car[j] = null;
						break;
					}
				}
			}
		}
		return true;
	}

	/**
	 * 借出车辆
	 */
	public boolean rent(String name, int date) {
		if (!checkCarName(name)) {
			System.out.println("没找到该车牌号!");
			return false;
		}
		for (int i = 0; i < car.length; i++) {
			if (car[i] != null && name.equals(car[i].name)) {
				if (car[i].state == true) {
					System.out.println("此车已租出！");
					return false;
				}
				car[i].state = true;
				car[i].date = date;
				car[i].count++;
			}
		}
		return true;
	}

	/**
	 * 借出车辆
	 */
	public boolean returnCar(String name) {
		if (!checkCarName(name)) {
			System.out.println("没找到该车牌号!");
			return false;
		}
		for (int i = 0; i < car.length; i++) {
			if (car[i] != null && name.equals(car[i].name)) {
				if (car[i].state == false) {
					System.out.println("此车未借出！");
					return false;
				}
				car[i].state = false;
				System.out.println(name + "日租金是" + car[i].price + ";租借时长：" + car[i].date + "天;\n总租金为"
						+ (car[i].price * car[i].date));
			}
		}
		return true;
	}

}
